package com.iris.live.services.common;/**
 * Created by Tony.Zhang on 2017/2/22.
 */

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:String工具类
 * @author: Tony.Zhang
 * @Date: 2017-02-22 20:13
 **/
public class StringUtils {
    /**
     * 字符串相等匹配（区分大小写）
     * @param val1
     * @param val2
     * @return
     */
    public static boolean equals(String val1, String val2){
        if(isNullOrBlank(val1) && isNullOrBlank(val2)){
            return true;
        }

        if(isNullOrBlank(val1) || isNullOrBlank(val2)){
            return false;
        }

        return val1.trim().equals(val2.trim());
    }

    /**
     * 字符串相等匹配（不区分大小写）
     * @param val1
     * @param val2
     * @return
     */
    public static boolean equalsIgnoreCase(String val1, String val2){
        if(isNullOrBlank(val1) && isNullOrBlank(val2)){
            return true;
        }

        if(isNullOrBlank(val1) || isNullOrBlank(val2)){
            return false;
        }

        return val1.trim().equalsIgnoreCase(val2.trim());
    }

    public static boolean isNotNullOrBlank(Object value){
        if(value == null) return false;

        return ! (value.toString().trim().equals("") || value.toString().trim().equalsIgnoreCase("null"));
    }


    public static boolean isNullOrBlank(Object value){
        if(value == null) return true;

        return value.toString().trim().equals("") || value.toString().trim().equalsIgnoreCase("null");
    }

    /**
     * 是否是整数数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        if(isNullOrBlank(str)){
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    /**
     * 是否是正数数字（不带加号）
     * @param str
     * @return
     */
    public static boolean isPositiveNumber(String str) {
        if (isNullOrBlank(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]+.?[0-9]*");
        Matcher matcher = pattern.matcher(str);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 动态检测参数是否为空
     * @param param
     * @return
     */
    public static boolean validataStrIsNull(String ...param){
        Integer nullNum=0;
        for(int i=0;i<param.length;i++){
            if (null==param[i] || ("").equals(param[i]) || ("null").equalsIgnoreCase(param[i])
                    || param[i].trim().isEmpty()){
                nullNum = nullNum +1;
            }
        }

        if (nullNum ==param.length){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 货币统一格式化入口
     * @param money
     * @return
     */
    public static String currencyFormat(Float money){
        return  new DecimalFormat("###,###,###.##").format(money );
    }

    /**
     * 格式化 Float数据
     * @param scale
     * @param roundingMode
     * @param ft
     * @return
     */
    public static float formatFloat(int scale,int roundingMode,Float ft){
        BigDecimal   bd  =   new BigDecimal((double)ft);
        bd   =  bd.setScale(scale,roundingMode);
        ft   =  bd.floatValue();
        return ft;
    }

    public static List<String> tokenize(String value, String seperator){
        List<String> values = new ArrayList<>();

        StringTokenizer st = new StringTokenizer(value, seperator);
        while(st.hasMoreElements()){
            values.add(st.nextElement().toString());
        }

        return values;
    }

    /**
     * 检测是否是公式导入
     * @param cell
     * @return
     */
    public static String getCellValue(Cell cell) {
        if (null == cell) {
            return null;
        }
        int cellType = cell.getCellType();
        String cellValue = "";
        switch (cellType) {
            case HSSFCell.CELL_TYPE_NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case HSSFCell.CELL_TYPE_FORMULA:
                try {
                    cellValue = cell.getStringCellValue();
                } catch (IllegalStateException e) {
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            default:
                cellValue = cell.getStringCellValue();
        }
        return cellValue.trim();
    }

    public static String lpad(String value, String c, int length) {
        StringBuffer sb = null;
        if (value == null) {
            sb = new StringBuffer("1");
        } else {
            sb = new StringBuffer(value);
        }

        while (sb.length() <= length) {
            sb.insert(0, c);
        }

        return sb.toString();
    }

    /**
     * 对象转为字符串类型
     * @param o
     * @return
     */
    public static String valueOf(Object o) {
        return o == null ? null : o.toString();
    }

    /**
     * 把小数转为百分比格式
     * @param str
     * @return
     */
    public static String getPercentValue(String str) {
        if (StringUtils.isNullOrBlank(str)) {
            return null;
        }
        return NumberUtils.round(Double.valueOf(str)*100,2) + "%";
    }


}
